package net.viservice.quartz.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import net.viservice.job.quartz.Task;
import net.viservice.quartz.job.PrintTimeJob;
import net.viservice.quartz.service.QuartzService;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class QuartzServiceImpl implements QuartzService {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final String className = "QuartzServiceImpl";

	@Resource
	private Scheduler scheduler;

	@Override
	public boolean scheduleJob(String jobGroup, String jobName, Task task, Map<String, Object> dataMap, String cronExpression) {
		String info = "添加任务";
		boolean result = false;

		try {
			// 查找任务最终执行对象
			Class jobClass = null;
			switch (task) {
			case PRINTCURRENTTIME:
				jobClass = PrintTimeJob.class;
				break;
			default:
				break;
			}
			
			JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroup).build();
			// 遍历参数，压入JobDateMap
			for (String key : dataMap.keySet()) {
				jobDetail.getJobDataMap().put(key, dataMap.get(key));
			}

			// 表达式调度构建器
			CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
			// 按新的cronExpression表达式构建一个新的trigger
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobDetail.getKey().getName(), jobDetail.getKey().getGroup()).withSchedule(cronScheduleBuilder).build();
			
			scheduler.scheduleJob(jobDetail, trigger);
			result = true;
		} catch (Exception e) {
			logger.error(className + ".scheduleJob(" + info + ") 发生异常", e);
		}

		return result;
	}

	@Override
	public boolean pauseJob(String jobGroup, String jobName) {
		String info = "暂停任务";
		boolean result = false;

		try {
			JobKey jobKey = new JobKey(jobName, jobGroup);
			scheduler.pauseJob(jobKey);
			logger.info(info + " ---> " + jobKey.toString());
			result = true;
		} catch (Exception e) {
			logger.error(className + ".pauseJob(" + info + ") 发生异常", e);
		}

		return result;
	}

	@Override
	public boolean resumeJob(String jobGroup, String jobName) {
		String info = "恢复任务";
		boolean result = false;

		try {
			JobKey jobKey = new JobKey(jobName, jobGroup);
			scheduler.resumeJob(jobKey);
			logger.info(info + " ---> " + jobKey.toString());
			result = true;
		} catch (Exception e) {
			logger.error(className + ".resumeJob(" + info + ") 发生异常", e);
		}

		return result;
	}

	@Override
	public boolean deleteJob(String jobGroup, String jobName) {
		String info = "删除任务";
		boolean result = false;

		try {
			JobKey jobKey = new JobKey(jobName, jobGroup);
			scheduler.deleteJob(jobKey);
			logger.info(info + " ---> " + jobKey.toString());
			result = true;
		} catch (Exception e) {
			logger.error(className + ".deleteJob(" + info + ") 发生异常", e);
		}

		return result;
	}

	@Override
	public boolean checkExists(String jobGroup, String jobName) {
		String info = "检查任务是否存在";
		boolean result = false;

		try {
			JobKey jobKey = new JobKey(jobName, jobGroup);
			logger.info(info + " ---> " + jobKey.toString());
			result = scheduler.checkExists(jobKey);
		} catch (Exception e) {
			logger.error(className + ".checkExists(" + info + ") 发生异常", e);
		}
		logger.info(info + " ---> result:" + result);
		return result;
	}

}
